package me.pplay.playtalk.activity;

import java.util.LinkedList;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.protocol.DataCallBack;
import me.pplay.playtalk.protocol.TpAuth;
import me.pplay.playtalk.protocol.bean.RspData;
import me.pplay.playtalk.util.LogManager;
import me.pplay.playtalk.util.PlayTalkUtil;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

public class SignUpSetQuestionActivity extends BaseActivity implements OnClickListener {
	private TpAuth tpAuth;
	private ImageButton signup_setquestion_btn;
	private EditText signup_setquestion_answer, signup_setquestion_email;
	private Spinner spinner;
	private ArrayAdapter<String> adapter;
	private String[] m;
	private int selectIndex = 0;
	String username="";
	String password="";
	private ProgressDialog progressDialog;
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case TpAuth.AUTH_CHANGEPWDPROTECTION_OK:
				login();
				break;
			case TpAuth.AUTH_CHANGEPWDPROTECTION_ERR: {
				LogManager.LogShow("SignUpSetQuestionActivity", "handleMessage AUTH_CHANGEPWDPROTECTION_ERR");
				progressDialog.dismiss();
				RspData rspdata = (RspData) msg.obj;
				showToast(rspdata.getString() == null ? "Set Protection Unkown err" : rspdata.getString());
			}
				break;
			case TpAuth.AUTH_CHANGEPWDPROTECTION_TIMEOUT:
				LogManager.LogShow("SignUpSetQuestionActivity", "handleMessage AUTH_CHANGEPWDPROTECTION_TIMEOUT");
				progressDialog.dismiss();
				showToast("Time out");
				break;
			case PlayTalkUtil.PLAYTALK_LOGIN_OK:
				progressDialog.dismiss();
				ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
				appState.closeAllActivity();
				Intent intent = new Intent(SignUpSetQuestionActivity.this, HomeActivity.class);
				startActivity(intent);
//				SignUpSetQuestionActivity.this.finish();
				break;
			case PlayTalkUtil.PLAYTALK_LOGIN_ERR: {
				progressDialog.dismiss();
				RspData rspdata = (RspData) msg.obj;
				showToast(rspdata.getString() == null ? "SignIn Unkown err" : rspdata.getString());
			}
				break;
			case PlayTalkUtil.PLAYTALK_LOGIN_TIMEOUT:
				progressDialog.dismiss();
				showToast("SignIn Time out");
				break;
			case PlayTalkUtil.PLAYTALK_LOGIN_WAITING:
				break;
			default:
				break;
			}
		}

	};

	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.signup_setquestion_btn:
			setquestionUp();
			break;
		default:
			break;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		LogManager.LogShow("SignUpSetQuestionActivity","onCreate");
		setContentView(R.layout.layout_signup_setquestion);
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		tpAuth = appState.getTpAuth();
		Bundle bundle=getIntent().getExtras();
		username = bundle.getString("username");
		password=bundle.getString("password");
		findViews();
	}
	private void login()
	{
		LogManager.LogShow("SignUpSetQuestionActivity","login" +username+" "+password);
		PlayTalkUtil.login((ApplicationTrans) getApplicationContext(), username, password,  new DataCallBack() {

			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				Message msg = new Message(); 
				msg.what = result; 
				msg.obj = resultdata;
				if (PlayTalkUtil.PLAYTALK_LOGIN_OK == result) {
					msg.obj = obj;
					LogManager.LogShow("SignUpSetQuestionActivity", "PLAYTALK_LOGIN_OK Globals.skyid = " + Globals.skyid);
				} else if (PlayTalkUtil.PLAYTALK_LOGIN_ERR == result) {
					LogManager.LogShow("SignUpSetQuestionActivity", "PLAYTALK_LOGIN_ERR");
				} else if (PlayTalkUtil.PLAYTALK_LOGIN_TIMEOUT == result) {
					LogManager.LogShow("SignUpSetQuestionActivity", "PLAYTALK_LOGIN_TIMEOUT");
				} else if (PlayTalkUtil.PLAYTALK_LOGIN_WAITING == result) {
					msg.obj = obj;
					LogManager.LogShow("SignUpSetQuestionActivity", "PLAYTALK_LOGIN_WAITING");
				}
				handler.sendMessage(msg);
			}
		});
	}
	/**
	 * Get View by viewId
	 */
	private void findViews() {
		signup_setquestion_btn = (ImageButton) findViewById(R.id.signup_setquestion_btn);
		signup_setquestion_answer = (EditText) findViewById(R.id.signup_setquestion_answer);
		signup_setquestion_email = (EditText) findViewById(R.id.signup_setquestion_email);
		signup_setquestion_btn.setOnClickListener(this);
		spinner = (Spinner) findViewById(R.id.signup_setquestion_spinner);
		LinkedList<String> list = tpAuth.tp_auth_get_pwdprotection_list();
		m = new String[list.size()];
		for(int i=0;i<list.size();i++){
			m[i] = list.get(i);
		}
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m);
		// 设置下拉列表的风格
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// 将adapter 添加到spinner中
		spinner.setAdapter(adapter);
		// 添加事件Spinner事件监听
		spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
		// 设置默认值
		spinner.setVisibility(View.VISIBLE);
	}

//	private void showToast(String str) {
//		Toast.makeText(SignUpSetQuestionActivity.this, str, Toast.LENGTH_SHORT).show();
//	}
	// 使用数组形式操作
	class SpinnerSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			selectIndex = arg2;
		}

		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}
	private void setquestionUp() {
		String question = tpAuth.tp_auth_get_pwdprotection_list().get(selectIndex);
		String answer = signup_setquestion_answer.getText().toString().trim();
		String email = signup_setquestion_email.getText().toString().trim();
		if (question.equals("") || answer.equals("")
				|| email.equals("") ) {
			showToast(getString(R.string.string_isnull));
			return;
		}
		progressDialog = ProgressDialog.show(this, getString(R.string.loading), getString(R.string.waittng), true, false); 
		progressDialog.show();

		tpAuth.tp_auth_change_pwdprotection(username,null, null, question, answer, email,new DataCallBack() {
			
			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				// TODO Auto-generated method stub
				Message msg = new Message();
				msg.what = result;
				msg.obj = resultdata;
				LogManager.LogShow("SignUpSetQuestionActivity", "Callback" + result);
				if (TpAuth.AUTH_CHANGEPWDPROTECTION_OK == result) {
					LogManager.LogShow("SignUpSetQuestionActivity", "AUTH_CHANGEPWDPROTECTION_OK");
					handler.sendMessage(msg);
				} else if (TpAuth.AUTH_CHANGEPWDPROTECTION_ERR == result) {
					LogManager.LogShow("SignUpSetQuestionActivity", "AUTH_CHANGEPWDPROTECTION_ERR");
					handler.sendMessage(msg);
				} else if (TpAuth.AUTH_CHANGEPWDPROTECTION_TIMEOUT == result) {
					LogManager.LogShow("SignUpSetQuestionActivity", "AUTH_CHANGEPWDPROTECTION_TIMEOUT");
					handler.sendMessage(msg);
				}
			}
		});
	}
}
